//
//  UIViewExtension.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/10.
//

import Foundation
import UIKit
extension UIView{
    // 放在draw方法执行
    // 添加下划线
    func addBottomBorder(_ lineColor:UIColor = Separator_COLOR,leftMargin:CGFloat = 0,rightMargin:CGFloat = 0){
        //获取绘图上下文
        guard let context = UIGraphicsGetCurrentContext() else {
            return
        }
        //创建一个矩形
        let drawingRect = self.bounds
        //创建并设置路径
        let path = CGMutablePath()
        path.move(to: CGPoint(x:0, y:drawingRect.height))
        path.addLine(to:CGPoint(x:drawingRect.width-(leftMargin+rightMargin), y:drawingRect.height))
        //添加路径到图形上下文
        context.addPath(path)
        //设置笔触颜色
        context.setStrokeColor(lineColor.cgColor)
        //设置笔触宽度
        context.setLineWidth(0.1)
        //绘制路径
        context.strokePath()
    }
    private struct AssociatedKey{
        static var isCircular = "isCircular"
        private init() {
            
        }
    }
    var isCircular:Bool{
        get{
            return objc_getAssociatedObject(self, &AssociatedKey.isCircular) as? Bool ?? false
        }
        set{
            objc_setAssociatedObject(self, &AssociatedKey.isCircular, newValue, .OBJC_ASSOCIATION_ASSIGN)
        }
    }
    
    func setCircularView() {
        self.layer.cornerRadius = frame.size.width/2
    }
    func setBorder(_ width:CGFloat = 1,color:UIColor = .white) {
        self.layer.borderColor = color.cgColor
        self.layer.borderWidth = width
    }
}
extension UIView{
    // 遍历视图的响应链，获取视图控制器viewController
    func fatherViewController() -> UIViewController? {
        var nextC = next
        while nextC != nil {
            if let nextObj = nextC as? UIViewController {
                return nextObj
            }
            nextC = nextC?.next
        }
        return nil
    }
    // 遍历视图的响应链，获取到navigationController
    func rootNavigationCntroller() -> UINavigationController? {
        var nextc = next
        while next != nil {
            if let nextObj = nextc as? UINavigationController {
                return nextObj
            }
            nextc = nextc?.next
        }
        return nil
    }
}
extension UIView{
    class func SeparatorLine()->UIView{
        let v = UIView()
        v.backgroundColor = Separator_COLOR
        return v
    }
}
extension UIView{
    func setRadiusAndDirection(radius:CGFloat,directions:[RadiusDirection]=[]){
        guard directions.count > 0 else {
            self.layer.cornerRadius = radius
            self.layer.masksToBounds = true
            return
        }
        var corners:Array<CACornerMask> = []
        var rectCorners:Array<UIRectCorner> = []
        for n in directions {
            switch n {
            case .topLeft:
                corners.append(CACornerMask.layerMinXMinYCorner)
                rectCorners.append(UIRectCorner.topLeft)
            case .topRight:
                corners.append(CACornerMask.layerMaxXMinYCorner)
                rectCorners.append(UIRectCorner.topRight)
            case .bottomLeft:
                corners.append(CACornerMask.layerMinXMaxYCorner)
                rectCorners.append(UIRectCorner.bottomLeft)
            case .bottomRight:
                corners.append(CACornerMask.layerMaxXMaxYCorner)
                rectCorners.append(UIRectCorner.bottomRight)
            }
        }
        if #available(iOS 11.0, *) {
            self.layer.maskedCorners = CACornerMask(corners)
            self.layer.cornerRadius = radius
        } else {
            // Fallback on earlier versions
            let shapeLayer = CAShapeLayer()
            shapeLayer.path = UIBezierPath.init(roundedRect: bounds, byRoundingCorners: UIRectCorner.init(rectCorners), cornerRadii: CGSize.init(width: radius, height: radius)).cgPath
            layer.mask = shapeLayer
        }
    }
}
enum RadiusDirection {
    case topLeft
    case topRight
    case bottomLeft
    case bottomRight
}
